﻿/*文件注释
 * ===============================================================================
 * 功能描述： ini文件读写
 * 作者：曾永佑
 * 版本：1.0.0.0 
 * 创建日期：2009-04-17
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace GitCapture
{
    /// <summary>
    /// ini文件读写
    /// </summary>
    public class IniVisitor
    {
        ////声明读写INI文件的API函数 
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        readonly string _inifile;
  
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="inifile">ini文件路径,用绝对路径</param>
        public IniVisitor(string inifile)
        {
            _inifile = inifile;
        }

        #region 成员方法

        /// <summary>
        /// 读ini文件,注意:ini文件必须是unicode字符
        /// </summary> 
        /// <param name="section">段</param>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        public T GetValue<T>(string section, string key)
        {
            var value = GetValue(section, key);
            return value != ""
                ? (T) Convert.ChangeType(value, typeof (T))
                : default(T);
        }

        /// <summary>
        /// 读ini文件,注意:ini文件必须是unicode字符
        /// </summary> 
        /// <param name="section">段</param>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        public string GetValue(string section, string key)
        {
            return GetValue(section, key, "");
        }
        /// <summary>
        /// 读ini文件,注意:ini文件必须是unicode字符
        /// </summary> 
        /// <param name="section">段</param>
        /// <param name="key">关键字</param>
        /// <param name="defaultvalue">默认值</param>
        /// <returns>返回数据值</returns>
        public string GetValue(string section, string key, object defaultvalue)
        {
            var temp = new StringBuilder(255);
            var i = GetPrivateProfileString(section, key, string.Empty, temp, 255, _inifile);
            if (i > 0)
                return temp.ToString();
           
            return defaultvalue == null ? "" : defaultvalue.ToString();
        }
        /// <summary>
        /// 写ini文件，注意:ini文件必须是unicode字符
        /// </summary> 
        /// <param name="section">段</param>
        /// <param name="key">关键字</param>
        /// <param name="value">值</param>
        /// <returns>返回写是否成功</returns>
        public bool SetValue(string section, string key, object value)
        {
            if (value != null)
            {
                return WritePrivateProfileString(section, key, value.ToString(), _inifile) != 0;
            }
            
            return false;
        }

        #endregion
    }  
}
